import type { ISMap } from './smap';

export interface IBaseKit {
  name: string;
  map: ISMap;
  status?: IBaseKitStatus;
  kitStatusChange: (status: IBaseKitStatus) => void;
  setStatus(status: IBaseKitStatus): void;
}

export type IBaseKitStatus = 'enable' | 'unenable';

export type IBaseKitOptions = Pick<IBaseKit, 'map' | 'name'>;

export class BaseKit implements IBaseKit {
  name: IBaseKit['name'];
  map: IBaseKit['map'];
  status: IBaseKit['status'];
  constructor(options: IBaseKitOptions) {
    this.name = options.name ?? 'unknown';
    this.map = options.map;
    this._bindEvent();
  }

  private _bindEvent() {
    this.map.on('kit.statusChange', (e) => {
      if (this.name !== e.name) this.kitStatusChange('unenable');
    });
  }

  protected onStatusChange() {
    throw 'Method not implemented.';
  }

  kitStatusChange(status: IBaseKitStatus) {
    if (this.status === status) return;
    this.status = status;
    this.map.fire('kit.statusChange', { status, name: this.name });
    this.onStatusChange();
  }

  setStatus(status: IBaseKitStatus) {
    this.kitStatusChange(status);
  }
}
